import { IFeidaoAiMobile } from '@dfeidao/atom-mobile/interfaces';
import get from '@dfeidao/atom-mobile/local/get';
import nodejs from '@dfeidao/atom-mobile/msg/nodejs';
import service from '@dfeidao/atom-mobile/msg/service';
import toast from '@dfeidao/atom-mobile/msgbox/toast';
import { spaceid } from '../atom/config';
import a001 from './a001';

export default async function a006(fd: IFeidaoAiMobile) {
	// 作业内容
	const contents_homework = get(fd, 'zynr');
	// 作业名称
	const quote_job_name = get(fd, 'zymc');
	// productid
	const productid = get(fd, 'productid');
	const teacher_no = get(fd, 'teacher_no');
	const teacher_name = get(fd, 'teacher_name');
	const cc = get(fd, 'obj') as {
		class_id: string;
		class_name: string;
		student_name: string;
		student_no: string
	};
	if (!cc || cc.student_name === '请选择' || !quote_job_name || !contents_homework) {
		await toast({
			text: '请填写信息！',
			type: 'warning',
			position: 'center'
		});
		return;
	}
	const student_name = cc.student_name;
	const student_no = cc.student_no;
	const class_id = cc.class_id;
	const class_name = cc.class_name;
	const message = "发布成功！";
	const r1 = await nodejs<{ code: number }>('app-personal-work/s002', { quote_job_name, contents_homework, student_name, student_no, productid, teacher_no, teacher_name, class_id, class_name });// 保存<作业,学生作业明细>表

	if (r1.code === 1) {
		a001(fd);
		const res = await nodejs<{ res: Array<{ parent_number: string, parent_name: string, phone: string }> }>('app-personal-work/s003', { productid, student_no });// 依据弹窗参数“学生编号”，是否消息接收人is_message_receiver”=“1”,查<学生家长>

		let parent_number = '';
		let parent_name = '';
		let phone = '';
		if (res.res.length > 0) {
			parent_number = res.res[0].parent_number;
			parent_name = res.res[0].parent_name;
			phone = res.res[0].phone;// 记得打开 18738555317 U0000026
		}

		if (!parent_number) {
			return toast({
				text: message + '未获取到家长信息',
				type: 'success',
				position: 'center'
			});
		}

		const r = await nodejs<{ openid: string, me: string }>
			('app-personal-work/s006', { productid, phone });
		if (r.me === '1') {
			return toast({
				text: message + '未获取到对应openid',
				type: 'success',
				position: 'center'
			});
		}
		const openid = r.openid;

		// 推送消息模板内容
		const information_template = await nodejs<Array<{ information_template_content: string }>>('app-personal-work/s004', { productid });// 据"信息模板类型" = "3","信息模板状态"="1" 查询<信息模板>表,得到"信息模板内容"
		if (information_template.length === 0) {
			return toast({
				text: message + '无信息模板',
				type: 'success',
				position: 'center'
			});
		}

		let information_template_content = information_template[0].information_template_content;
		information_template_content = information_template_content.replace("@", parent_name);
		information_template_content = information_template_content.replace("@", student_name);
		// console.log(information_template_content, '------模板内容----');

		const d = await service("thirdpart.weixinpublicno", {
			"serviceflag": "getaccesstoken",      // 固定参数
			"spaceid": spaceid,           // 当前项目的spaceid
			"thirdpay_type": "weixin_pubno", // 固定参数（weixin_pubno）
			"productid": productid    // 当前登录用户对应的productid（从系统参数params中获取）
		}) as { access_token: string };
		// console.log(d.access_token, '------d-------');

		try {
			const result = await service('thirdpart.weixinpubno.bulkpushmessage', {
				"tousers": [
					openid/// res1中的openid
				],
				"spaceid": spaceid,
				"productid": productid,
				"access_token": d.access_token, /// 获取的token
				"custombody": {
					"msgtype": 'text', /// 推送的消
					'text': {
						'content': information_template_content
					}
				}
			}) as { code: number, msg: string };
			if (result.code > 0) {
				return toast({
					text: message + "已通知家长！",
					type: 'success',
					position: 'center'
				});
			}
		} catch (error) {
			return toast({
				text: message + '推送接口异常',
				type: 'success',
				position: 'center'
			});
		}
	} else {
		await toast({
			text: '发布失败，请稍后重试！',
			type: 'danger',
			position: 'center'
		});
	}
}
